package com.hmall.search.listener;

import com.hmall.feign.constants.MQConstants;
import com.hmall.search.service.ISearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ItemListener {

    @Autowired(required = false)
    private ISearchService searchService;

    //监听：新增或修改的队列
    @RabbitListener(queues = MQConstants.ITEM_INSERT_QUEUE)
    public void listenInsertOrUpdate(Long id){
        try {
            searchService.insertById(id);
            log.info("ES数据同步文档"+id+"：插入或更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("ES数据同步文档"+id+"：插入或更新失败，"+e.getMessage());
        }
    }

    //监听：删除的操作队列
    @RabbitListener(queues = MQConstants.ITEM_DELETE_QUEUE)
    public void listenDelete(Long id){
        try {
            searchService.deleteById(id);
            log.info("ES数据同步文档"+id+"：删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("ES数据同步文档"+id+"：删除失败，"+e.getMessage());
        }
    }
}